//
//  AppDelegate.m
//  B
//
//  Created by 王展 on 15/11/4.
//  Copyright © 2015年 wangzhan. All rights reserved.
//

#import "AppDelegate.h"

#import "MyTabBar.h"
#import "RecommendViewController.h"

#import "WeatherViewController.h"
#import "OtherViewController.h"
#import "MyViewController.h"

#import "MusicDetailViewController.h"

@interface AppDelegate ()<otherHidedelegate>

@property(nonatomic,retain)MyTabBar *myTB;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [self setWindow:[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]]];
    [[self window] makeKeyAndVisible];
    // addObserver:　观察者，即在什么地方接收通知;
    // selector:　收到通知后调用何种方法;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeColor:) name:@"changeColor" object:nil];
    NSString *str = [[NSUserDefaults standardUserDefaults]objectForKey:@"night"];
    if ([str isEqualToString:@"YES"]) {
        self.window.alpha = 0.5;
    }
    
    
    [self createAppView];
    
    return YES;
}
-(void)changeColor:(NSNotification *)notification
{
    self.window.alpha = 0.5;
    NSString *str = [notification object];
    if ([str isEqualToString:@"NO"]) {
        self.window.alpha = 1.0;
    }
    
    [[NSUserDefaults standardUserDefaults]setObject:str forKey:@"night"];
}
//开始创建
-(void)createAppView
{
    [[DataBaseHandle ShareDataBaseHandle]openDB];
   
    [[DataBaseHandle ShareDataBaseHandle]createImageCollect];
    
    //创建推荐页面
    RecommendViewController *recommendVC = [RecommendViewController alloc];
    recommendVC.view.backgroundColor = [UIColor greenColor];
    UINavigationController *recommendNC = [[UINavigationController alloc] initWithRootViewController:recommendVC];
    recommendNC.navigationBar.translucent = NO;
    //创建目的地页面
    DestinationViewController *destinationVC = [[DestinationViewController alloc] init];
    destinationVC.view.backgroundColor = [UIColor redColor];
    destinationVC.myDelegate = self;
    UINavigationController *destinationNC = [[UINavigationController alloc] initWithRootViewController:destinationVC];
    destinationNC.navigationBar.translucent = NO;
    //创建天气页面
    WeatherViewController *weatherVC = [[WeatherViewController alloc] init];
    weatherVC.view.backgroundColor = [UIColor whiteColor];
    UINavigationController *weatherNC = [[UINavigationController alloc] initWithRootViewController:weatherVC];
    weatherNC.navigationBar.translucent = NO;
    //创建其他页面
    OtherViewController *otherVC = [[OtherViewController alloc] init];
    otherVC.view.backgroundColor = [UIColor purpleColor];
    UINavigationController *otherNC = [[UINavigationController alloc] initWithRootViewController:otherVC];
    otherNC.navigationBar.translucent = NO;
    otherVC.myAgent = self;
    //创建我的页面
    MyViewController *myVC = [[MyViewController alloc] init];
    //myVC.view.backgroundColor = [UIColor yellowColor];
    UINavigationController *myNC = [[UINavigationController alloc] initWithRootViewController:myVC];
    myNC.navigationBar.translucent = NO;

    self.myTB = [[MyTabBar alloc] init];
    self.myTB.viewControllers = [NSArray arrayWithObjects:recommendNC, destinationNC, weatherNC, otherNC, myNC, nil];
    self.myTB.tabBar.translucent = NO;
    self.window.rootViewController = self.myTB;
}
-(void)hidemyBottomBar
{
    UIView *view = [self.myTB.view.subviews objectAtIndex:1];
    view.hidden = YES;
}
-(void)appearmyBottomBar
{
    UIView *view = [self.myTB.view.subviews objectAtIndex:1];
    view.hidden = NO;
}

-(void)hideOtherBottomBar
{
    UIView *view = [self.myTB.view.subviews objectAtIndex:1];
    view.hidden = YES;
}
-(void)appearOhterBottomBar
{
    UIView *view = [self.myTB.view.subviews objectAtIndex:1];
    view.hidden = NO;
}














-(void)remoteControlReceivedWithEvent:(UIEvent *)event
{
    if (event.type == UIEventTypeRemoteControl) {
        
        if (event.subtype == UIEventSubtypeRemoteControlPlay) {
            
            [[MusicDetailViewController sharePlayerViewController] startButtonAction];
            
        }else if (event.subtype == UIEventSubtypeRemoteControlPause)
        {
            
            [[MusicDetailViewController sharePlayerViewController] startButtonAction];
            
        }
        else if (event.subtype == UIEventSubtypeRemoteControlNextTrack){
            
            [[MusicDetailViewController sharePlayerViewController] nextButtonAction];
            
            [[MusicDetailViewController sharePlayerViewController] newMusicInfo];
            
            
        }else if (event.subtype == UIEventSubtypeRemoteControlPreviousTrack)
            
            [[MusicDetailViewController sharePlayerViewController] lastButtonAction];
        
        [[MusicDetailViewController sharePlayerViewController] newMusicInfo];
        
    }
    
}






- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
